# 完全重启前端开发服务器
# 用法：在项目根目录运行 powershell -ExecutionPolicy Bypass -File restart-dev.ps1

Write-Host "================================" -ForegroundColor Cyan
Write-Host "  重启前端开发服务器" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""

# 步骤1：结束所有node进程
Write-Host "步骤 1/4: 结束所有 Node.js 进程..." -ForegroundColor Yellow
try {
    Get-Process -Name node -ErrorAction SilentlyContinue | Stop-Process -Force
    Write-Host "✓ Node.js 进程已结束" -ForegroundColor Green
} catch {
    Write-Host "! 没有找到运行中的 Node.js 进程" -ForegroundColor Gray
}
Start-Sleep -Seconds 1

# 步骤2：清除缓存
Write-Host ""
Write-Host "步骤 2/4: 清除缓存目录..." -ForegroundColor Yellow
if (Test-Path "node_modules\.cache") {
    Remove-Item -Recurse -Force "node_modules\.cache"
    Write-Host "✓ 已删除 node_modules\.cache" -ForegroundColor Green
} else {
    Write-Host "! node_modules\.cache 不存在" -ForegroundColor Gray
}

if (Test-Path "dist") {
    Remove-Item -Recurse -Force "dist"
    Write-Host "✓ 已删除 dist" -ForegroundColor Green
} else {
    Write-Host "! dist 目录不存在" -ForegroundColor Gray
}

# 步骤3：显示配置信息
Write-Host ""
Write-Host "步骤 3/4: 检查配置..." -ForegroundColor Yellow
if (Test-Path ".env") {
    $envContent = Get-Content ".env" | Select-String "VUE_APP_BASE_API"
    Write-Host "✓ .env 配置: $envContent" -ForegroundColor Green
} else {
    Write-Host "✗ 找不到 .env 文件！" -ForegroundColor Red
}

# 步骤4：启动开发服务器
Write-Host ""
Write-Host "步骤 4/4: 启动开发服务器..." -ForegroundColor Yellow
Write-Host ""
Write-Host "======================================" -ForegroundColor Cyan
Write-Host "  即将启动，请注意查看终端日志" -ForegroundColor Cyan
Write-Host "  应该看到 [代理请求] 和 [代理响应]" -ForegroundColor Cyan
Write-Host "======================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "按 Ctrl+C 可以停止服务" -ForegroundColor Gray
Write-Host ""

# 启动npm服务
npm run serve
